/*
 * Remove all rows and columns that contain only zeros in a symmetric
 * matrix.
 * Outputs the resulting matrix on stdout (one row per line).
 * Outputs the row numbers (which are equal to the row numbers) of the rows
 * that were removed on stderr, where the first row (column) in the matrix
 * is numbered as 0.
 * Takes no arguments.
 */
#include <iostream>
#include <sstream>
#include <string>
#include "u1.h"
using namespace std;

int main(int argc, char **argv)
{
  // Read matrix into memory, one row at a time.
  vector<vector<double> > m;
  vector<double> row;
  string line;
  size_t cols = -1U; // not portable, strictly speaking 
  while (getline(cin,line)) {
    // Process row.
    istringstream is(line);
    double entry;
    while (is >> entry) {
      row.push_back(entry);
    }

    // Sanity check: make sure all rows have same # of entries.
    if (cols == -1U) {
      cols = row.size();
    } else if (row.size() != cols) {
      cerr<<"Row "<<m.size()+1<<" contains "<<row.size()
        <<" columns; expected "<<cols<<"."<<endl;
      exit(EXIT_FAILURE);
    }

    m.push_back(row);
    row.clear();
  }

  // Find rows and columns that are entirely zero.
  // We just scan the whole matrix once and update a vector of booleans
  // every time we see a non-zero entry.
  vector<bool> zero(m.size(), true);
  for (size_t i = 0; i < m.size(); ++i) {
    for (size_t j = 0; j < m[i].size(); ++j) {
      if (m[i][j] != 0.0) {
        zero[i] = false;
        zero[j] = false;
      }
    }
  }

  // Output matrix, skipping zero rows and columns).
  for (size_t i = 0; i < m.size(); ++i) {
    if (zero[i]) continue;
    for (size_t j = 0; j < m[i].size(); ++j) {
      if (zero[j]) continue;
      cout<<m[i][j]<<" ";
    }
    cout<<endl;
  }

  // Output ordinals of zero rows (columns).
  for (size_t i = 0; i < m.size(); ++i) {
    if (zero[i]) cerr<<i<<'\n';
  }

  return EXIT_SUCCESS;
}

/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

